package com.goudan.practice.array;

public class MaxMatrix {

    public static void main(String[] args) {
        int[][] matrix = {{1, 0, 1, 0, 0}, {1, 0, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 0, 0, 1, 0}};

        int side = 0;
        int ros = matrix.length;
        int col = matrix[0].length;
        for (int i = 0; i < ros; i++) {
            for (int j = 0; j < col; j++) {
                if (matrix[i][j] == 1) {

                    int maxSide = Math.min(ros-i, col-j);
                    for (int k = 1; k < maxSide; k++) {
                        //判断后续的行和列是否都是1
                        boolean flag = true;
                        if (matrix[i + k][j + k] == 0) break;
                        for (int m = 0; m < k; m++) {
                            if (matrix[i + k][j + m] == 0 || matrix[i + m][j + k] == 0) {
                                flag = false;
                                break;
                            }
                        }
                        if (flag) {
                            maxSide = Math.max(maxSide, k + 1);
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        System.out.print(side * side);
    }
}
